#include <stdlib.h>
#include <stdio.h>
#include "myfunc.h"

int main(int argc, char **argv)
{
    Buffer buf; /* A buffer */
    unsigned char *blk;
    int i, j, k, total_IO = 0;
    initBuffer(520, 64, &buf);
    for (i = 1; i <= 48; i++)
    {
        blk = readBlockFromDisk(i, &buf);
        qsort(blk, 7, 8, Compare);
        writeBlockToDisk(blk, BASE2 + i, &buf);
    }
    total_IO += buf.numIO;
    freeBuffer(&buf);
    // sort R and S
    for (i = 0; i < 6; i++)
    {
        Merge_8(200 + i * 8, &total_IO);
    }
    Merge_16_R(200, &total_IO);
    Merge_32_S(216, &total_IO);
    printf("total IO in sort:%d\n", total_IO);
    struct Tuple tuple;
    int base = 300;
    for (i = 1; i <= 6; i++)
    {
        if (i == 1)
        {
            printf("------ R ------\n");
        }
        if (i == 3)
        {
            printf("------ S ------\n");
            base = 400 - 16;
        }
        for (j = 1; j <= 8; j++)
        {
            blk = readBlockFromDisk(base + j + (i - 1) * 8, &buf);
            for (k = 0; k < 7; k++)
            {
                GetTuple(&tuple, k, blk);
                printf("(%d,%d)  ", tuple.first, tuple.second);
            }
            freeBlockInBuffer(blk, &buf);
            printf("\n");
        }
    }
    return 0;
}